package com.itlong.cloud.utils.random;

import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.utils.encrypt.Md5EncryptUtil;

import java.util.Random;
import java.util.UUID;

/**
 * <desc>
 *      业务逻辑ID处理。
 * </desc>
 *
 * @createDate 2017/9/19
 */
public class LogicIdUtil {


    final static IdWorker idWorker = new IdWorker(10);

    /**
     * 随机获取业务逻辑ID
     *
     * @return 业务逻辑编码ID，18位长度
     */
    public static String bussinessId() {
        long bussinessId = idWorker.nextId();
        return String.valueOf(bussinessId);
    }

    public static long bussinessIdLong() {
        long bussinessId = idWorker.nextId();
        return bussinessId;
    }

    /**
     * <desc>
     *      获取用户加密逻辑id
     * </desc>
     *
     * @return
     */
    public static String userBusinessId(){
        long bussinessId = idWorker.nextId();
        return Md5EncryptUtil.md5(String.valueOf(bussinessId) + PlatformConstants.BUSINESS_ID_MD5_KEY);
    }

    /**
     * 随机获取一个唯一的UUID
     */
    public static String randomUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * <desc>
     *      生成32位隨機數
     * </desc>
     *
     * @return
     * @Jiaqi.X
     * @createDate 2019/08/23
     */
    public static String randomNum(){
        Random rand = new Random();
        StringBuffer sb=new StringBuffer();
        for (int i=1;i<=20;i++){
         int randNum = rand.nextInt(9)+1;
         String num=randNum+"";
        sb=sb.append(num);
        }
        String random=String.valueOf(sb);
        return random;
    }

    /**
     * 获取项目的随机密钥(appsecret)
     * 16位长度，0-9与A-F，交替混合
     *
     * @return
     * @throws Exception
     */
    public static String getProjectKey() throws Exception {
        Random random = new Random();
        int numSize = random.nextInt(16);
        String pubKey = getNumRandom(numSize, 16 - numSize);
        while (pubKey.length() != 16) {
            numSize = random.nextInt(16);
            pubKey = getNumRandom(numSize, 16 - numSize);
        }
        return pubKey;
    }


    /**
     * 生产项目的随机密钥,16位长度，0-9与A-F
     *
     * @param numSize  数字个数
     * @param charSize 字符串个数
     * @return
     */
    public static String getNumRandom(int numSize, int charSize) {
        String hopeChar = "";
        Random random = new Random();
        String[] charArr = new String[]{"A", "B", "C", "D", "E", "F"};
        if (numSize > charSize) {
            for (int j = 0; j < numSize - 1; j++) {
                if (j <= charSize) {
                    hopeChar += random.nextInt(10) + charArr[random.nextInt(charArr.length)];
                } else {
                    hopeChar += random.nextInt(10);
                }
            }
        } else {
            for (int j = 0; j < charSize - 1; j++) {
                if (j <= numSize) {
                    hopeChar += charArr[random.nextInt(charArr.length)] + random.nextInt(10);
                } else {
                    hopeChar += random.nextInt(10);
                }
            }
        }
        return hopeChar;
    }

}
